package huawei.js2025;

/**
 * 幸运数字
 */
public class Question1 {

    /**
     * 转换为N进制
     * @return
     */
    public String convertNum(Integer sourceNum, Integer targetRadix) {
        StringBuilder sb = new StringBuilder();

        while (sourceNum > 0) {
            int yu = sourceNum % targetRadix;
            sourceNum /= targetRadix;
            sb.insert(0, yu > 9 ? ((char) ('A' + (yu - 10))) + "" : yu);
        }

        return sb.toString();
    }

    // N进制转换为10进制
    public Integer convertToInt(String s, Integer radix) {
        int sum = 0;
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);

            int num = 0;
            if (c >= 'A' && c <= 'Z') {
                num = 10 + (c - 'A');
            } else {
                num = c - '0';
            }


            sum += (int) (num * Math.pow(radix, s.length() - i - 1));
        }

        return sum;
    }

    public int isLuckyNum(Integer k, Integer n, Integer m) {
        int res = 0;
        char n1 = (char) (n + '0');
        String s = convertNum(k, m);
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == n1) {
                res += 1;
            }
        }
        return res;
    }

    public static void main(String[] args) {
        int k = 20;
        int n = 1;
        int m = 21;

        Question1 q = new Question1();
        String s = q.convertNum(k, m);
        System.out.println(s);

        int s1 = q.convertToInt(s, m);
        System.out.println(s1);

        System.out.println(q.isLuckyNum(k, n, m));
    }
}
